/**
 * 数据库种子数据初始化
 */

import mongoose from 'mongoose';
import config from '@config/index';
import database from '@config/database';
import logger from '@utils/logger';
import { seedUsers } from './001-create-admin-user';
import { seedKnowledge } from './002-create-demo-knowledge';

/**
 * 运行所有种子数据
 */
export async function runAllSeeds(): Promise<void> {
  try {
    logger.info('Starting database seeding...');

    // 按顺序执行种子脚本
    await seedUsers();
    await seedKnowledge();

    logger.info('Database seeding completed successfully');
  } catch (error) {
    logger.error('Database seeding failed:', error as Error);
    throw error;
  }
}

/**
 * 独立运行脚本
 */
if (require.main === module) {
  (async () => {
    try {
      // 连接数据库
      await database.connectMongoDB();
      logger.info('Connected to MongoDB');

      // 运行种子数据
      await runAllSeeds();

      logger.info('All seeds completed successfully');
      process.exit(0);
    } catch (error) {
      logger.error('Seeding process failed:', error as Error);
      process.exit(1);
    }
  })();
}
